package Test;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class File1 extends JFrame implements ActionListener{
    private File file;
    JMenuItem saveAsMenuItem;
    PersonJFrame personJFrame;
    PersonJPanel personJPanel;

    public File1(Person[] pers, PersonJPanel personJPanel) {
        personJFrame = new PersonJFrame(pers,personJPanel);
        this.personJPanel = personJPanel;
    }

    public void createAndShowGUI() {

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("文件");

        // 创建菜单项
        JMenuItem newMenuItem = new JMenuItem("新建");
        JMenuItem openMenuItem = new JMenuItem("打开");
        JMenuItem saveMenuItem = new JMenuItem("保存");
        saveAsMenuItem = new JMenuItem("另存为");

        // 添加菜单项事件监听器
        newMenuItem.addActionListener(this);
        openMenuItem.addActionListener(this);
        saveMenuItem.addActionListener(this);
        saveAsMenuItem.addActionListener(this);
        // 添加菜单项到菜单
        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(saveAsMenuItem);
        // 添加菜单到菜单栏
        menuBar.add(fileMenu);
        // 添加菜单栏到窗口
        this.personJFrame.setJMenuBar(menuBar);
    }

    @Override
    public void actionPerformed(ActionEvent e){
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(personJPanel);
        if(e.getActionCommand().equals("新建")){
            file = null;
            personJFrame.tableModel.setRowCount(0);
        }else if(e.getActionCommand().equals("打开")){
            if (result == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
                try (FileReader fileReader = new FileReader(file)) {
                    BufferedReader bufferedReader = new BufferedReader(fileReader);
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        sb.append(line).append("\n");
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else if(e.getActionCommand().equals("保存")){
                if (file != null) {
                    try (FileWriter fileWriter = new FileWriter(file)) {
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    saveAsMenuItem.doClick();
                }
            }else if(e.getActionCommand().equals("另存为")){
                if (result == JFileChooser.APPROVE_OPTION) {
                    file = fileChooser.getSelectedFile();
                    try (FileWriter fileWriter = new FileWriter(file)) {
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Person[] pers = {new Person("zhang","2020年10月10号","男","江苏","扬州"),
                new Person("lisi","2023年10月11号","男","江苏","扬州")};
        File1 f = new File1(pers,new PersonJPanel());
        f.createAndShowGUI();
    }
}
